
package main.chessimo.training;

import java.util.ArrayList;
import java.util.List;


public class TrainingFunctions {
    
    
    public String[] getUnitFiles (int unit) {
        
        String strUnits = getUnitString(unit);
        String tab[] = strUnits.split("-");
        
        return tab;
     
    }
    
 
    public static void main(String[] args) {
    	getTime("TAT", 51, 121, 180);
	}
    
    
    
    public static int getCountFileDone(String module, int unit, int diagram){

    	int pointer = 0;
    	
    	if (diagram % getExercsOnFile(module) == 0) {
			pointer = (diagram / getExercsOnFile(module)) - 1;
		}else{
			pointer = (diagram / getExercsOnFile(module));
		}
    	
//    	int pointer = (int) Math.abs((diagram / getExercsOnFile(module))) - 1;

    	String arquivosDaUnidade[] = getUnitString(unit).split("-");

    	int arquivoAtual = Integer.parseInt(arquivosDaUnidade[pointer]);

    	List<Integer> arquivosFeitos = new ArrayList<Integer>();
    	//todos os arquivos que ja foram feitos da unidade atual
    	for (int i = 0; i < arquivosDaUnidade.length; i++) {
    	     if (i <= pointer) {
    	    	 arquivosFeitos.add(Integer.valueOf(arquivosDaUnidade[i]));
    	     } 
    	}

    	//todos os arquivos que ja foram feitos das unidades anteriores
    	for (int i = 1; i < unit; i++) {
    		String[] splitArquivosUnidade = getUnitString(i).split("-");
    		
    		for (String cadaArquivo : splitArquivosUnidade) {
    			arquivosFeitos.add(Integer.valueOf(cadaArquivo));
			}
    	} 	

    	int count = 0;
    	for (int i = 0; i < arquivosFeitos.size(); i++) {
    	   if (arquivoAtual == arquivosFeitos.get(i)) {
    	      count++;
    	   }
    	}
    	
    	return count;
    }
    
    public static int getExercsOnFile(String module){
    	if (module != null) {
	    	if (module.contains("TAT")) {
				return 15;
			}else if (module.contains("STR")) {
				return 5;
			}else if (module.contains("END")) {
				return 10;
			}else {
				return 0;
			}
    	}
    	return 0;
    }
    
    
    public static int getTime (String module, int unit, int diagram, int baseTime) {
        
        int time = 0;
      
        int count = getCountFileDone(module, unit, diagram);
       
        if (count == 1) {
            time = baseTime;
        } else if (count == 2 || count == 3) {
            time = baseTime / 2;
        } else if (count == 4 || count == 5) {
            time = baseTime / 3;
        } else if (count == 6) {
            time = baseTime / 4;
        } else {
            time = baseTime / 6;
        }
        
        if ("STR".equals(module.substring(0,3))) {
            time = time * 2;
        }
        System.out.println(time);
        return time;
        
    }    
        
    public static int getTotalExeUnit(String module, int unit){
    	String unitString = getUnitString(unit);
    	String[] files = unitString.split("-");
    	return files.length * getExercsOnFile(module);
    }
    
        
    public static String getUnitString (int unit) {
        
        String[] units = new String[52];
        
        units[0]  = "";
        units[1]  = "1-2-1-2";
        units[2]  = "3-4-1-2-3-4";
        units[3]  = "5-6-3-4-5-6";
        units[4]  = "7-8-5-6-7-8";
        units[5]  = "9-10-7-8-9-10";
        units[6]  = "11-12-9-10-11-12";
        units[7]  = "13-14-11-12-13-14";
        units[8]  = "15-16-13-14-1-2-3-15-16";
        units[9]  = "17-18-15-16-4-5-6-17-18";
        units[10] = "19-20-17-18-7-8-9-19-20";
        units[11] = "21-22-19-20-10-11-12-21-22";
        units[12] = "23-24-21-22-1-2-3-4-23-24";
        units[13] = "5-6-7-8-23-24";
        units[14] = "9-10-11-12-13-14-15";
        units[15] = "1-2-3-4-16-17-18";
        units[16] = "5-6-7-8-19-20-21";
        units[17] = "9-10-11-12-22-23-24";
        units[18] = "1-2-3-4-5-6-13-14-15-16";
        units[19] = "7-8-9-10-11-12-17-18-19-20";
        units[20] = "1-2-3-4-5-6-7-8-9-10-11-12-21-22-23-24";
        units[21] = "25-26-13-14-15-16-25-26";
        units[22] = "27-28-17-18-29-20-25-26-27-28";
        units[23] = "29-30-21-22-23-24-27-28-29-30";
        units[24] = "31-32-13-14-15-16-17-18-29-30-31-32";
        units[25] = "33-34-19-20-21-22-23-24-31-32-33-34";
        units[26] = "35-36-13-14-15-16-17-18-19-20-21-22-23-24-33-34-35-36";
        units[27] = "37-38-35-36-37-38";
        units[28] = "39-40-25-26-27-37-38-39-40";
        units[29] = "41-42-28-29-30-39-40-41-42";
        units[30] = "43-44-31-32-33-41-42-43-44";
        units[31] = "45-46-34-35-36-43-44-45-46";
        units[32] = "47-48-25-26-27-28-45-46-47-48";
        units[33] = "29-30-31-32-47-48";
        units[34] = "33-34-35-36-37-38-39";
        units[35] = "25-26-27-28-40-41-42";
        units[36] = "29-30-31-32-43-44-45";
        units[37] = "33-34-35-36-46-47-48";
        units[38] = "25-26-27-28-29-30-37-38-39-40";
        units[39] = "31-32-33-34-35-36-41-42-43-44";
        units[40] = "25-26-27-28-29-30-31-32-33-34-35-36-45-46-47-48";
        units[41] = "37-38-39-40";
        units[42] = "41-42-43-44";
        units[43] = "45-46-47-48";
        units[44] = "37-38-39-40-41-42";
        units[45] = "43-44-45-46-47-48";
        units[46] = "37-38-39-40-41-42-43-44-45-46-47-48";
        units[47] = "1-2-3-4-5-6-7-8-9-10-11-12";
        units[48] = "13-14-15-16-17-18-19-20-21-22-23-24";
        units[49] = "25-26-27-28-29-30-31-32-33-34-35-36";
        units[50] = "37-38-39-40-41-42-43-44-45-46-47-48";
        units[51] = "1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34-35-36-37-38-39-40-41-42-43-44-45-46-47-48";
        
        return units[unit];
        
    }  
    
    
    public static String getTrainingLabel(String module, int unit, int exerc) {
    	 	
    	if (module.equals("TAT-01") || module.equals("STR-01") || module.equals("END-01") || module.equals("COM-01") || module.equals("OPE-01")) {
			if (unit == 1 && exerc == 0) {
				return "INICIAR";
			}
    	} 
    	
    	return "CONTINUAR";
    	
    }
    
    
    
    
    
 
    
}
